package com.atguigu.orderservice.controller;


import com.atguigu.commonutils.Result;
import com.atguigu.orderservice.service.PayLogService;
import com.baomidou.mybatisplus.extension.api.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.ObjectStreamClass;
import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author haoyue1573
 * @since 2023-03-24
 */
@RestController
@RequestMapping("/orderservice/log")
@CrossOrigin
public class PayLogController {

	@Resource
	private PayLogService payService;
	/**
	 * 生成二维码
	 * @return
	 */
	@GetMapping("/createNative/{orderNo}")
	public Result createNative(@PathVariable String orderNo) {
		Map<String, String> aNative = payService.createNative(orderNo);
		//返回的是订单的结果集
		return Result.ok().data("native",aNative);
	}

	/**
	 * 查询订单的状态
	 * @param orderNo
	 * @return
	 */
	@GetMapping("/queryPayStatus/{orderNo}")
	public Result queryPayStatus(@PathVariable String orderNo) {
		//调用查询接口
		Map<String, String> map = payService.queryPayStatus(orderNo);
		if (map == null) {//出错
			return Result.error().message("支付出错");
		}
		if (map.get("trade_state").equals("SUCCESS")) {//如果成功
			//更改订单状态
			payService.updateOrderStatus(map);
			return Result.ok().message("支付成功");
		}

		return Result.ok().code(25000).message("支付中");
	}

}

